package com.bitters.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Created with IntelliJ IDEA
 * Created By thomas
 * Date: 2019/5/9
 * Description ${description}
 * Time: 16:12
 */
@Component
public class SpringbootKafkaListener {

    private Logger logger = LoggerFactory.getLogger(SpringbootKafkaListener.class);

    @KafkaListener(
            topics = {"kafka1"},
            groupId = "springboot-kafka-consumer"
    )
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            this.logger.info("Receive： +++++++++++++++ Topic:kafka1");
            this.logger.info("Receive： +++++++++++++++ Record:" + record);
            this.logger.info("Receive： +++++++++++++++ Message:" + message);
        }

    }
}
